import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)
import LRLayout from '@/layout/LRLayout'
import { LRLayoutRouters } from './LRLayout'
import { UDLayoutRouters } from './UDLayout'

// 加密解密开始
import crypto from '../utils/publicUtils/crypto' // 引入

function deepCopy(obj) {
  if (obj instanceof Object) {
    const newObj = {}
    if (Array.isArray(obj)) {
      const arr = []
      obj.forEach(item => {
        arr.push(deepCopy(item))
      })
      return arr
    } else {
      for (const key in obj) {
        const value = obj[key]
        if (typeof value === 'function') {
          newObj[key] = value.bind(newObj)
        } else if (typeof value === 'object') {
          if (Array.isArray(value)) {
            newObj[key] = []
            value.forEach(item => {
              newObj[key].push(deepCopy(item))
            })
          } else {
            newObj[key] = deepCopy(value)
          }
        } else {
          newObj[key] = value
        }
      }
    }
    return newObj
  } else {
    return obj
  }
}

const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) { return originalPush.call(this, location, onResolve, onReject) }
  // 解决空对象中含有{__ob__: Observer}，深拷贝
  let newLocation = deepCopy(location)
  // let newLocation = location
  if (newLocation?.query && Object.keys(newLocation.query).length !== 0) {
    newLocation = location.query?.__params
        ? location
        : {
          ...location,
          query: {
            __params: crypto.Encrypt(JSON.stringify(newLocation.query))
          }
        }
  }
  return originalPush.call(this, newLocation)
}

const originalRoe = Router.prototype.resolve
Router.prototype.resolve = function resolve(location, onResolve, onReject) {
  if (onResolve || onReject) { return originalRoe.call(this, location, onResolve, onReject) }
  // 解决空对象中含有{__ob__: Observer}，深拷贝
  let newLocation = deepCopy(location)
  // let newLocation = location
  if (newLocation?.query && Object.keys(newLocation.query).length !== 0) {
    newLocation = location.query?.__params
        ? location
        : {
          ...location,
          query: {
            __params: crypto.Encrypt(JSON.stringify(newLocation.query))
          }
        }
  }
  return originalRoe.call(this, newLocation)
}
// 加密解密结束

export const constantRoutes = [
  ...LRLayoutRouters,
  ...UDLayoutRouters,
  // 重定向页面路由 勿删
  {
    path: '/redirect',
    component: LRLayout,
    hidden: true,
    children: [{
      path: '/redirect/:path(.*)',
      component: resolve => require(['@/views/redirect'], resolve)
    }]
  },
  /* login 页面有两种
   * 1：login--bao--Y ==> 包姐原有
   * 2：login--hecaitong-zhang ==> 合财通抽出(包含账号、手机号验证码、二维码等三种登录方式及是否多租户选择，默认不需要租户，如果后去需要租户打开相关注释即可)
   */
  {
    path: '/login',
    component: resolve => require(['@/views/login/index.vue'], resolve),
    hidden: true,
    meta: {
      title: '登录'
    }
  },
  {
    path: '/404',
    component: resolve => require(['@/views/404'], resolve),
    hidden: true,
    meta: {
      title: '404'
    }
  }
]

/**
 * asyncRoutes
 * the routes that need to be dynamically loaded based on user roles
 */
export const asyncRoutes = []

const createRouter = () => {
  return new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({
      y: 0
    }),
    routes: constantRoutes
  })
}

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}
export default router
